package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //判断添加的菜品或者套餐在购物车中是否已经存在
        List<ShoppingCart> list = shoppingCartMapper.select(shoppingCart);


        if (list != null && !list.isEmpty()){
            //要添加的菜品或者套餐在购物车中 已经 存在

            //修改菜品或者套餐数量
            shoppingCart = list.get(0);

            Integer number = shoppingCart.getNumber();
            shoppingCart.setNumber(number + 1);

            //更新购物车
            shoppingCartMapper.updateNumberById(shoppingCart);
        }else {
            //要添加的菜品或者套餐在购物车中 不 存在

            //判断是套餐还是菜品
            if (shoppingCart.getDishId() != null){
                //添加的是菜品

                //根据 dish_id 查询菜品
                Dish dish = dishMapper.selectById(shoppingCart.getDishId());

                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());

            }
            else {
                //添加的是套餐
                //根据 dish_id 查询菜品
                Setmeal setmeal = setmealMapper.seleteById(shoppingCart.getSetmealId());

                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());

            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = new ShoppingCart();
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        return shoppingCartMapper.select(shoppingCart);
    }

    /**
     * 清空购物车
     */
    @Override
    public void delete() {

        Long userId = BaseContext.getCurrentId();

        shoppingCartMapper.delete(userId);
    }
}

